<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="自定义逻辑维护" @del="del" @newCreate="newCreate"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
            @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'自定义逻辑维护'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'ediCustomProcess',
  data() {
    return {
      dialogTableVisible:false,
      tableSelectData: [],
      gridData:[],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        id: null,
        name: '',
        code: '',
        type: ''
      },
      options: {
        ediCustomProcess: []
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          disabled: true,
          invalid: 'del'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '自定义逻辑名称',
          prop: 'name',
          input: 'input'
        },
        {
          label: '描述',
          prop: 'descr',
          input: 'input'
        },
        {
          label: '类型',
          prop: 'type',
          input: 'select',
          options: [
          { label: '校验', value: 'VALIDATE' },
          { label: '保存或更新前', value: 'PREHANDLE' },
          { label: '保存或更新后', value: 'CUSTOMAFTER' }
          ]
        },
        {
          label: 'springbean名称',
          prop: 'beanname',
          input: 'input'
        },
        {
          label: '执行顺序0-100 数字越小越先执行',
          prop: 'priority',
          input: 'input'
        }
      ]
    },
    formConfig() {
      return [
        {
          label: '自定义逻辑名称',
          prop: 'name',
          type: 'input'
        },
        {
          label: '类型',
          prop: 'type',
          type: 'select',
          options:  [
          { label: '校验', value: 'VALIDATE' },
          { label: '保存或更新前', value: 'PREHANDLE' },
          { label: '保存或更新后', value: 'CUSTOMAFTER' },
          { label: '字段级别', value: 'FIELD' }
          ]
        },
        {
          label: '描述',
          prop: 'descr',
          type: 'input'
        },
        {
          label: 'springbean名称',
          prop: 'beanname',
          type: 'select',
          options:this.options.ediCustomProcess
        }, 
        {
          label: '执行顺序0-100 数字越小越先执行',
          prop: 'priority',
          type: 'input'
        }
      ]
    },
    formRules() {
      return {

      }
    },
    apiMap() {
      return {
        getListApi: '/v1/ediCustomProcess/selectEdiCustomProcessList',
        getInfoApi:'/v1/ediCustomProcess/selectEdiCustomProcessById',
        editApi:'/v1/ediCustomProcess/editEdiCustomProcess',
        deleteApi: '/v1/ediCustomProcess/deleteEdiCustomProcess',
        addApi: '/v1/ediCustomProcess/addEdiCustomProcess'
      }
    }
  },
  methods: {
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)

        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(this.apiMap.editApi, params)
        } else {
          promise = this.$post(this.apiMap.addApi, params)
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.$refs.detail?.resetFields()
          this.dialogModule.flag = false
        })
      })

    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, {
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.name}]`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增自定义逻辑'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    getOptions() {
      this.$post('/v1/ediCustomProcess/queryEdiCustomProcess',{}).then(res => {
        this.options.ediCustomProcess = res.data.item;
      })
    },

    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {
    this.getOptions();
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
